<template>
    <div class="home" ref="homeRef">
        <!-- 头部 -->
        <div class="navBar">
            <div class="title">弘源旅途</div>
        </div>
        <!-- 轮播图 -->
        <div class="banner">
            <img src="@/assets/img/home/banner.webp" alt="">
        </div>
        <!-- 搜索框区域 -->
        <HomeSearch></HomeSearch>
        <!-- 推荐类别 -->
        <HomeCategories></HomeCategories>
        <!-- 滑动后显示搜索框 -->
        <div class="search-bar" v-if="isShowSearchBar">
            <SearchBar></SearchBar>
        </div>
        <!-- 热门精选 -->
        <HomeHotSelected></HomeHotSelected>
        <!-- 加载更多 -->
        <!-- <div style="margin: 10px 20px; margin-bottom: 80px;">
            <van-button  round color="var(--theme-linear-gradient)" block @click="moreHouseList">加载更多</van-button>
        </div> -->
    </div>
</template>

<script>
export default{
    name: "home"
}
</script>


<script setup>
import HomeSearch from './components/homeSearch.vue';
import HomeCategories from './components/homeCategories.vue';
import HomeHotSelected from './components/homeHotSelected.vue';
import SearchBar from '../../components/searchBar/searchBar.vue'
import useHomeStore from '@/stores/modules/home';
import { onMounted, onUnmounted, watch, ref, computed, onActivated } from 'vue';
import useScroll from '@/hooks/useScroll'

// 发送网络请求获取热门建议、推荐类别
const homeStore = useHomeStore()
homeStore.getHomeHotSuggests()
homeStore.getHomeCategories()
homeStore.getHouseList()

// 1、点击加载更多
// const moreHouseList = () => {
//     console.log("加载更多的数据")
//     homeStore.getHouseList()
// }

// 2、到底部自动加载更多
// 监听window的创建的滚动，当我们离开页面时, 我们移除监听，
// 如果别的页面也进行类似的监听, 会编写重复代码
// const scrollListenerHandler = () => {
//   const clientHeight = document.documentElement.clientHeight
//   const scrollTop = document.documentElement.scrollTop
//   const scrollHeight = document.documentElement.scrollHeight
//   if (clientHeight + scrollTop >= scrollHeight) {
//     console.log("滚动到底部")
//     homeStore.getHouseList()
//   }
// }

// onMounted(() => {
//     window.addEventListener("scroll", scrollListenerHandler)
// })

// onUnmounted(() => {
//     window.removeEventListener("scroll", scrollListenerHandler)
// })

// 3、封装hook传入回调函数
// useScroll(() => {
//     homeStore.getHouseList()
// })

// 4、封装hook,watch监听（是否到底部）
const homeRef = ref()
const { isReachBottom, scrollTop } = useScroll(homeRef)
watch(isReachBottom, (newvalue) => {
    if(newvalue){
        homeStore.getHouseList().then(() => {
            isReachBottom.value = false
        })
    }
})

// 跳转回home时, 保留原来的位置
onActivated(() => {
    homeRef.value?.scrollTo({
        top: scrollTop.value
    })
})

// 5、搜索框显示的控制
// const isShowSearchBar = ref(false)
// watch(scrollTop, (newTop) => {
//     if(newTop > 100){
//         isShowSearchBar.value = true
//     }
// })

// 定义的可响应式数据, 依赖另外一个可响应式的数据, 那么可以使用计算函数(computed)
const isShowSearchBar = computed(() => {
    return scrollTop.value > 100
})


</script>

<style lang="less" scoped>
.home{
    height: 100vh;
    overflow-y: auto;
    box-sizing: border-box; //把padding-bottom计算到 height: 100vh中, 不然就是100vh + 60px
    padding-bottom: 60px;
}

// 头部
.navBar {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 46px;
    border-bottom: 1px solid #f2f2f2;

    .title {
        color: var(--primary-color);
        font-size: 16px;
        font-weight: 600;
    }
}

// 轮播图
.banner {
    img {
        width: 100%;
    }
}

//搜索框
.search-bar {
  position: fixed;
  z-index: 9;
  top: 0;
  left: 0;
  right: 0;
  height: 45px;
  padding: 16px 16px 10px;
  background-color: #fff;
}
</style>